﻿package 
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	import samples.BaseSample;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Base extends Sprite 
	{
		
		private var sampleId:int = 0;
		private var tf:TextFormat;
		private var txt:TextField;
		
		private var sample:BaseSample;
		
		public function Base():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			_stage = stage;
			_stage.align = StageAlign.TOP_LEFT;
			_stage.scaleMode = StageScaleMode.NO_SCALE;
			_stage.displayState = StageDisplayState.FULL_SCREEN;
			_stage.addEventListener( KeyboardEvent.KEY_DOWN, keyHandler )
			
			txt = new TextField();
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.selectable = false;
			txt.antiAliasType = AntiAliasType.ADVANCED;
			addChild( txt );
			
			tf = new TextFormat( 'Verdana', 16, 0xFFFFFF );
			sampleId = 0;
			keyHandler();
		}
		
		private function keyHandler(e:KeyboardEvent = null ):void 
		{
			if ( 	e != null
			&&		e.keyCode != Keyboard.RIGHT 
			&&		e.keyCode != Keyboard.LEFT 
			&&		e.keyCode != Keyboard.UP ) return;
			
			if ( e != null )
			{
				switch( e.keyCode )
				{
					case Keyboard.RIGHT:
					sampleId++;
					break;
					
					case Keyboard.LEFT:
					sampleId--;
					break;
					
				}
			}
			
			if ( sampleId < 0) sampleId = 0;
			
			var n:int = numChildren;
			while ( n-- )
			{
				if( !( getChildAt( n ) is TextField ) ) removeChildAt( n );
			}
			
			var description:String = '';
			var op:int = 0;
			switch( sampleId )
			{
				case op++:
				//
				break;
			}
			
			txt.text = sampleId.toString() +' : ' + description;
			txt.setTextFormat( tf );
			if( sample != null ) addChild( sample );
			
		}
		
	}
	
}